﻿using LxBasic;
using LxCore;

namespace LxCore
{
    public abstract class RefUserPara
    {
        public abstract class Base<T> : ParameterBase<T>
        {
            public UserPara.Base<T> UserDefPara { get; set; }
            public override T GetPara()
            {
                return UserDefPara.Parameter.GetPara();
            }
            public override ICellVarArr<T> GetCellVarArr(IIndexHost indexHost)
            {
                return UserDefPara.Parameter.GetCellVarArr(indexHost);
            }
            public override IFaceVarArr<T> GetFaceVarArr(IIndexHost indexHost)
            {
                return UserDefPara.Parameter.GetFaceVarArr(indexHost);
            }
        }

        public class Double : Base<double>
        {
            public Double() { }
            public Double(string userDefParaName)
            {
                UserDefParaName = userDefParaName;
            }
            public string UserDefParaName
            {
                get => UserDefPara?.Name;
                set
                {
                    UserDefPara = LxSim.Sim.Definitions.UserPara.Scalar[value];
                    if (UserDefPara == null) { throw new InvalidDataException($"未找到 {value} 对应的数据"); }
                }
            }
            public override void FromJsonKV(string key, IJsonReader jsReader)
            {
                switch (key)
                {
                    case nameof(UserDefPara): UserDefParaName = jsReader.ReadString(); break;
                    default: base.FromJsonKV(key, jsReader); break;
                }
            }
            public override void ToJsonKV(IJsonWriter writer)
            {
                base.ToJsonKV(writer);
                writer.WriteString(nameof(UserDefPara), UserDefPara.Name);
            }
        }
        public class Vector : Base<Vector3d>
        {
            public Vector() { }
            public Vector(string userDefParaName)
            {
                UserDefParaName = userDefParaName;
            }
            public string UserDefParaName
            {
                get => UserDefPara?.Name;
                set
                {
                    UserDefPara = LxSim.Sim.Definitions.UserPara.Vector[value];
                    if (UserDefPara == null) { throw new InvalidDataException($"未找到 {value} 对应的数据"); }
                }
            }
            public override void FromJsonKV(string key, IJsonReader jsReader)
            {
                switch (key)
                {
                    case nameof(UserDefPara): UserDefParaName = jsReader.ReadString(); break;
                    default: base.FromJsonKV(key, jsReader); break;
                }
            }
            public override void ToJsonKV(IJsonWriter writer)
            {
                base.ToJsonKV(writer);
                writer.WriteString(nameof(UserDefPara), UserDefPara.Name);
            }
        }

    }
}
